package dto

import (
	"gorm.io/gorm"
	"project2/g"
	"project2/model"
	"project2/utils"
	"time"
)

type SysRoleDto struct {
	Id           uint      `json:"id"`
	Code         string    `json:"code"`
	Name         string    `json:"name"`
	ParentId     uint      `json:"parentId"`
	ParentIdPath string    `json:"parentIdPath"`
	Description  string    `json:"description"`
	Img          string    `json:"img"`
	Deep         uint      `json:"deep"`
	OrderNum     uint      `json:"orderNum"`
	TypeInfo     string    `json:"typeInfo"`
	TenantType   uint      `json:"tenantType"`
	TenantId     uint      `json:"tenantId"`
	DelState     uint      `json:"delState"`
	AddUser      string    `json:"addUser"`
	AddUserId    uint      `json:"addUserId"`
	AddTime      time.Time `json:"addTime"`
	EditUser     string    `json:"editUser"`
	EditUserId   uint      `json:"editUserId"`
	LastEditTime time.Time `json:"lastEditTime"`
	ServerNumber uint      `json:"serverNumber"`
}

func BuildSysRoleDto(_body *map[string]interface{}, dto *SysRoleDto) {
	body := *_body
	if body == nil {
		return
	}

	if body["id"] != nil && utils.ParserInt8(body["id"]) > 0 {
		dto.Id = uint(utils.ParserInt8(body["id"]))
	}

	if body["code"] != nil && body["code"].(string) != "" {
		dto.Code = body["code"].(string)
	}

	if body["name"] != nil && body["name"].(string) != "" {
		dto.Name = body["name"].(string)
	}

	if body["parentId"] != nil && utils.ParserInt8(body["parentId"]) > 0 {
		dto.ParentId = uint(utils.ParserInt8(body["parentId"]))
	}

	if body["parentIdPath"] != nil && body["parentIdPath"].(string) != "" {
		dto.ParentIdPath = body["parentIdPath"].(string)
	}

	if body["description"] != nil && body["description"].(string) != "" {
		dto.Description = body["description"].(string)
	}

	if body["img"] != nil && body["img"].(string) != "" {
		dto.Img = body["img"].(string)
	}

	if body["deep"] != nil && utils.ParserInt8(body["deep"]) > 0 {
		dto.Deep = uint(utils.ParserInt8(body["deep"]))
	}

	if body["orderNum"] != nil && utils.ParserInt8(body["orderNum"]) > 0 {
		dto.OrderNum = uint(utils.ParserInt8(body["orderNum"]))
	}

	if body["typeInfo"] != nil && body["typeInfo"].(string) != "" {
		dto.TypeInfo = body["typeInfo"].(string)
	}

	if body["tenantType"] != nil && utils.ParserInt8(body["tenantType"]) > 0 {
		dto.TenantType = uint(utils.ParserInt8(body["tenantType"]))
	}

	if body["tenantId"] != nil && utils.ParserInt8(body["tenantId"]) > 0 {
		dto.TenantId = uint(utils.ParserInt8(body["tenantId"]))
	}

	if body["delState"] != nil && utils.ParserInt8(body["delState"]) > 0 {
		dto.DelState = uint(utils.ParserInt8(body["delState"]))
	}

	if body["addUser"] != nil && body["addUser"].(string) != "" {
		dto.AddUser = body["addUser"].(string)
	}

	if body["addUserId"] != nil && utils.ParserInt8(body["addUserId"]) > 0 {
		dto.AddUserId = uint(utils.ParserInt8(body["addUserId"]))
	}

	if body["addTime"] != nil && !utils.Str2date(body["addTime"].(string)).IsZero() {
		dto.AddTime = utils.Str2date(body["addTime"].(string))
	}

	if body["editUser"] != nil && body["editUser"].(string) != "" {
		dto.EditUser = body["editUser"].(string)
	}

	if body["editUserId"] != nil && utils.ParserInt8(body["editUserId"]) > 0 {
		dto.EditUserId = uint(utils.ParserInt8(body["editUserId"]))
	}

	if body["lastEditTime"] != nil && !utils.Str2date(body["lastEditTime"].(string)).IsZero() {
		dto.LastEditTime = utils.Str2date(body["lastEditTime"].(string))
	}

	if body["serverNumber"] != nil && utils.ParserInt8(body["serverNumber"]) > 0 {
		dto.ServerNumber = uint(utils.ParserInt8(body["serverNumber"]))
	}

}
func (self *SysRoleDto) BuildQuery(query **gorm.DB) {
	(*query) = g.Db.Model(&model.SysAccount{})
	(*query).Where("del_time is not null")

	if self.Id > 0 {
		(*query).Where("id =?", self.Id)
	}

	if self.Code != "" {
		(*query).Where("code =?", self.Code)
	}

	if self.Name != "" {
		(*query).Where("name =?", self.Name)
	}

	if self.ParentId > 0 {
		(*query).Where("parent_id =?", self.ParentId)
	}

	if self.ParentIdPath != "" {
		(*query).Where("parent_id_path =?", self.ParentIdPath)
	}

	if self.Description != "" {
		(*query).Where("description =?", self.Description)
	}

	if self.Img != "" {
		(*query).Where("img =?", self.Img)
	}

	if self.Deep > 0 {
		(*query).Where("deep =?", self.Deep)
	}

	if self.OrderNum > 0 {
		(*query).Where("order_num =?", self.OrderNum)
	}

	if self.TypeInfo != "" {
		(*query).Where("type_info =?", self.TypeInfo)
	}

	if self.TenantType > 0 {
		(*query).Where("tenant_type =?", self.TenantType)
	}

	if self.TenantId > 0 {
		(*query).Where("tenant_id =?", self.TenantId)
	}

	if self.DelState > 0 {
		(*query).Where("del_state =?", self.DelState)
	}

	if self.AddUser != "" {
		(*query).Where("add_user =?", self.AddUser)
	}

	if self.AddUserId > 0 {
		(*query).Where("add_user_id =?", self.AddUserId)
	}

	if !self.AddTime.IsZero() {
		(*query).Where("add_time =?", self.AddTime)
	}

	if self.EditUser != "" {
		(*query).Where("edit_user =?", self.EditUser)
	}

	if self.EditUserId > 0 {
		(*query).Where("edit_user_id =?", self.EditUserId)
	}

	if !self.LastEditTime.IsZero() {
		(*query).Where("last_edit_time =?", self.LastEditTime)
	}

	if self.ServerNumber > 0 {
		(*query).Where("server_number =?", self.ServerNumber)
	}

}
func (self *SysRoleDto) BuildEntityForAdd(entity *model.SysRole) {

	if self.Id > 0 {
		entity.Id = self.Id
	} else {
		entity.Id = 0
	}

	if self.Code != "" {
		entity.Code = self.Code
	} else {
		entity.Code = ""
	}

	if self.Name != "" {
		entity.Name = self.Name
	} else {
		entity.Name = ""
	}

	if self.ParentId > 0 {
		entity.ParentId = self.ParentId
	} else {
		entity.ParentId = 0
	}

	if self.ParentIdPath != "" {
		entity.ParentIdPath = self.ParentIdPath
	} else {
		entity.ParentIdPath = ""
	}

	if self.Description != "" {
		entity.Description = self.Description
	} else {
		entity.Description = ""
	}

	if self.Img != "" {
		entity.Img = self.Img
	} else {
		entity.Img = ""
	}

	if self.Deep > 0 {
		entity.Deep = self.Deep
	} else {
		entity.Deep = 0
	}

	if self.OrderNum > 0 {
		entity.OrderNum = self.OrderNum
	} else {
		entity.OrderNum = 0
	}

	if self.TypeInfo != "" {
		entity.TypeInfo = self.TypeInfo
	} else {
		entity.TypeInfo = ""
	}

	if self.TenantType > 0 {
		entity.TenantType = self.TenantType
	} else {
		entity.TenantType = 0
	}

	if self.TenantId > 0 {
		entity.TenantId = self.TenantId
	} else {
		entity.TenantId = 0
	}

	if self.DelState > 0 {
		entity.DelState = self.DelState
	} else {
		entity.DelState = 0
	}

	if self.AddUser != "" {
		entity.AddUser = self.AddUser
	} else {
		entity.AddUser = ""
	}

	if self.AddUserId > 0 {
		entity.AddUserId = self.AddUserId
	} else {
		entity.AddUserId = 0
	}

	if !self.AddTime.IsZero() {
		entity.AddTime = self.AddTime
	} else {
		entity.AddTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.EditUser != "" {
		entity.EditUser = self.EditUser
	} else {
		entity.EditUser = ""
	}

	if self.EditUserId > 0 {
		entity.EditUserId = self.EditUserId
	} else {
		entity.EditUserId = 0
	}

	if !self.LastEditTime.IsZero() {
		entity.LastEditTime = self.LastEditTime
	} else {
		entity.LastEditTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.ServerNumber > 0 {
		entity.ServerNumber = self.ServerNumber
	} else {
		entity.ServerNumber = 0
	}

}
func (self *SysRoleDto) BuildEntityForUpdate(entity *model.SysRole) {
	entity.Id = self.Id

	if self.Id > 0 {
		entity.Id = self.Id
	}

	if self.Code != "" {
		entity.Code = self.Code
	}

	if self.Name != "" {
		entity.Name = self.Name
	}

	if self.ParentId > 0 {
		entity.ParentId = self.ParentId
	}

	if self.ParentIdPath != "" {
		entity.ParentIdPath = self.ParentIdPath
	}

	if self.Description != "" {
		entity.Description = self.Description
	}

	if self.Img != "" {
		entity.Img = self.Img
	}

	if self.Deep > 0 {
		entity.Deep = self.Deep
	}

	if self.OrderNum > 0 {
		entity.OrderNum = self.OrderNum
	}

	if self.TypeInfo != "" {
		entity.TypeInfo = self.TypeInfo
	}

	if self.TenantType > 0 {
		entity.TenantType = self.TenantType
	}

	if self.TenantId > 0 {
		entity.TenantId = self.TenantId
	}

	if self.DelState > 0 {
		entity.DelState = self.DelState
	}

	if self.AddUser != "" {
		entity.AddUser = self.AddUser
	}

	if self.AddUserId > 0 {
		entity.AddUserId = self.AddUserId
	}

	if !self.AddTime.IsZero() {
		entity.AddTime = self.AddTime
	} else {
		entity.AddTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.EditUser != "" {
		entity.EditUser = self.EditUser
	}

	if self.EditUserId > 0 {
		entity.EditUserId = self.EditUserId
	}

	if !self.LastEditTime.IsZero() {
		entity.LastEditTime = self.LastEditTime
	} else {
		entity.LastEditTime = utils.Str2date("1970-01-01 00:00:00")
	}

	if self.ServerNumber > 0 {
		entity.ServerNumber = self.ServerNumber
	}

}
